<template>
  <div class="message-container mt20 document-center">
    <div class="header">
      消息列表
    </div>
    <div class="content" v-loading="isLoading">
      <div v-if="list && list.length" class="messageList">
        <div class="messageItem" v-for="(item,index) in list" :key="index">
          <el-tooltip :disabled="item.message && !item.isTextTruncated" class="item" effect="dark" :content="item.message" placement="top">
            <span :ref="`title_${index}`" class="title">{{item.message}}</span>
          </el-tooltip>
          <span class="time">{{item.updateTime && $moment(item.updateTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
        </div>
      </div>
      <div v-else class="document-center flex-c">
        <el-empty description="暂无数据" :image-size="300"></el-empty>
      </div>
    </div>

    <div class="flex-c mt20">
      <el-pagination background :current-page="searchParams.currentPage" :page-size="searchParams.pageSize" @size-change="handleSizeChange" @current-change="handleCurrentChange"
        layout="prev, pager, next, jumper,->,total" :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: 'message',
  data () {
    return {
      searchParams: {
        userId: sessionStorage.userId,
        currentPage: 1,
        pageSize: 8
      },
      list: [],
      total: 0,
      isLoading: false
    };
  },
  methods: {
    async getList (page) {
      this.isLoading = true;
      if (page) this.searchParams.currentPage = page;
      this.Request.getDrillMessageList({ params: this.searchParams }).then((res) => {
        this.list = res.data;
        this.total = res.total;
        this.$nextTick(() => {
          if (this.list && this.list.length) {
            this.list.forEach((item, index) => {
              const titleSpan = this.$refs[`title_${index}`];
              item.isTextTruncated = (titleSpan && titleSpan[0]) && titleSpan[0].scrollWidth > titleSpan[0].clientWidth;
            });
          }
        });
      }).finally(() => {
        this.isLoading = false;
      });
    },
    handleSizeChange (val) {
      this.searchParams.pageSize = val;
      this.getList(1);
    },
    handleCurrentChange (val) {
      this.searchParams.currentPage = val;
      this.getList();
    }
  },
  mounted () {
    this.getList(1);
  }
};
</script>

<style lang="scss" scoped>
@import "@/css/variable.scss";
.message-container {
  margin-top: 0.2rem;
  .header {
    font-size: 30px;
    font-weight: 500;
    color: $white5;
    height: 70px;
    // margin-bottom: 30px;
  }
  .content {
    .messageList {
      border-top: 1px solid #eee;
      border-bottom: 1px solid #eee;
      .messageItem:not(:last-child) {
        border-bottom: 1px solid #eee;
      }
      .messageItem {
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        height: 80px;
        line-height: 80px;
        font-size: 18px;
        .title {
          width: 1000px;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }
        .time {
          color: #666;
        }
      }
    }
  }
  .el-pagination {
    width: 100%;
    text-align: center;
  }
}
</style>
